<template>
  <div class="dashboard-container">
    <page-tools>
      <template slot="after">
        <button type="primary" @click="addPermission(0, 1)">
          添加
        </button></template
      >
    </page-tools>
    <!-- 指定id为 唯一属性的标识 -->
    <el-table border :data="list" row-key="id">
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column
        label="标识"
        prop="code"
        align="center"
      ></el-table-column>
      <el-table-column
        label="描述"
        prop="description"
        align="center"
      ></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            size="mini"
            v-if="row.type === 1"
            @click="addPermission(row.id, 2)"
            >添加</el-button
          >
          <el-button size="mini" @click="editPermission(row.id)"
            >编辑</el-button
          >
          <el-button size="mini" @click="delClick(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增和编辑弹层 -->
    <el-dialog :visible.sync="showDialog" :title="title">
      <!-- 表单 -->
      <el-form :model="permformData" :rules="rules" ref="permformData">
        <el-form-item label="名称" label-width="100px" prop="name">
          <el-input style="width: 70%" v-model="permformData.name"></el-input>
        </el-form-item>
        <el-form-item label="标识" label-width="100px" prop="code">
          <el-input style="width: 70%" v-model="permformData.code"></el-input>
        </el-form-item>
        <el-form-item label="描述" label-width="100px">
          <el-input
            style="width: 70%"
            v-model="permformData.description"
          ></el-input>
        </el-form-item>
        <!-- 开关 -->
        <el-form-item label-width="100px" label=" 开启">
          <!-- 当1是开 -->
          <el-switch
            v-model="permformData.enVisible"
            active-value="1"
            inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>

      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { tranListToTreeData } from '@/utils'
import {
  updatePermission,
  addPermission,
  getPermissionDetail,
  delPermission,
  getPermissionList
} from '@/api/permission'
export default {
  data() {
    return {
      list: [],
      showDialog: false, //控制弹层的显示
      permformData: {
        enVisible: '0', //开启 0
        name: '', //名称
        code: '', //标识
        description: '', //  描述
        type: '', //  类型
        pid: '' //  节点
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  computed: {
    title() {
      if (this.permformData.id) {
        return '编辑'
      } else {
        return '添加'
      }
    }
  },
  methods: {
    // 获取权限列表
    async getPermissionList() {
      // 修改数据
      this.list = tranListToTreeData(await getPermissionList(), '0')
      // console.log(this.list)
    },

    // 删除
    delClick(id) {
      // console.log(id)
      this.$confirm('确认要永久删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await delPermission(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getPermissionList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    addPermission(pid, type) {
      ;(this.permformData.type = type),
        (this.permformData.pid = pid),
        (this.showDialog = true)
    },
    async editPermission(id) {
      // 数据回显
      this.permformData = await getPermissionDetail(id)
      // console.log(this.permformData)  //  这个里边会有 id
      this.showDialog = true
    },
    async btnOK() {
      if (this.permformData.id) {
        // 编辑
        await updatePermission(this.permformData)
        this.$message.success('修改成功')
        this.showDialog = false
      } else {
        // 添加
        this.$refs.permformData.validate(async (valid) => {
          if (valid) {
            await addPermission(this.permformData)
            this.$message.success('新增成功')
            this.getPermissionList()
            this.showDialog = false
            this.permformData = {}
          } else {
            this.$message.error('务必填写完整')
            return false
          }
        })
      }
    },
    //     async btnOK() {
    //   // 校验成功
    //   this.$refs.permformData
    //     .validate()
    //     .then(() => {
    //       if (this.permformData.id) {
    //         return updatePermission(this.permformData)
    //       }
    //       // 添加成功
    //       return addPermission(this.permformData)
    //     })
    //     .then(() => {
    //       //  提示消息
    //       this.$message.success('新增成功')
    //       this.getPermissionList()
    //       this.showDialog = false
    //     })
    // },
    btnCancel() {
      // 关闭弹出层
      this.showDialog = false
      // 清空数据
      this.permformData = {}
      // 移除校验
      this.$refs.permformData.resetFields()
    }
  }
}
</script>

<style>
</style>